const { User } = require('../../model/user');

module.exports = async (req, res, next) => {
    let id = req.query.id;
    let body = req.body;
    let user = await User.findOne({_id: id});

    // 判断用户输入的原密码是否正确    如果正确就把用户输入的信息更新到数据库  否则提示用户密码错误
    if (user.password == body.password) {
        // 然后把数据更新进数据库
        await User.update({_id: id}, {
            name: body.name,
            email: body.email,
            password: body.newPassword,    // 把新密码传输给password
            role: body.role,
            state: body.state
        });
        res.redirect('/admin/user');
    } else {
        next(JSON.stringify({path: '/admin/user-edit', message: '您输入的原密码有误，修改失败', id: id}));
    };
};